var paramMap = new Map();
$(function() {
	$("#btn_add_param")
			.click(
					function() {
						$("#param_name_group").removeClass(
								"control-group error");
						$("#param_name_group").addClass("controls");
						var fun_param_name = $("#fun_param_name").val().trim();
						if (fun_param_name == "") {
							$("#param_name_group").addClass(
									"control-group error");
							return false;
						} else {
							if (paramMap.containsKey(fun_param_name)) {
								$("#param_name_group").addClass(
										"control-group error");
								return false;
							}
							var param = $("#param_type option:selected").text()
									.trim()
									+ " " + fun_param_name;
							$("#fun_param_name").val("");
							paramMap.put(fun_param_name, "");
							$("#param_list")
									.append(
											'<div class="controls">'
													+ '<input type="hidden" id="param_type_temp" value="'
													+ $(
															"#param_type option:selected")
															.val().trim()
													+ '">'
													+ '<input type="hidden" id="param_name_temp" value="'
													+ fun_param_name
													+ '">'
													+ '<code id="param_code">'
													+ param
													+ '</code>'
													+ '<button style="margin-left:50px;margin-bottom:5px" type="button" class="btn" onclick="deltr(this)">删除</button></div>');
						}
						$("#param_name_group").removeClass(
								"control-group error");
						$("#param_name_group").addClass("controls");
					});
});
function deltr(delbtn) {
	var temp = $(delbtn).parents(".controls").children("#param_name_temp").val();
	paramMap.remove(temp);
	$(delbtn).parents(".controls").remove();
}
function add_interface() {
	$("#addInterface").dialog('open');
}
function refreshAddDiv() {
	$("#param_name_group").removeClass("controls error");
	$("#param_name_group").addClass("controls");
	$("#fun_name_group").removeClass("controls error");
	$("#fun_name_group").addClass("controls");
	$("#param_list").html("");
	paramMap = new Map();
}

function addFunction() {
	var fun_name = $("#fun_name").val().trim();
	if (fun_name == "") {
		$("#fun_name_group").addClass("control-group error");
		return false;
	}
	var fun_params = "";
	var fun_params_id = "";
	var params = new Array();
	var paramsId = new Array();
	$("#param_list .controls").each(function() {
		var param = $(this).children("#param_code").text().trim();
		params.push(param);
		var type_id = $(this).children("#param_type_temp").val().trim();
		paramsId.push(type_id);

	});
	
	fun_params = params.join(";");
	fun_params_id = paramsId.join(";");
	//alert(fun_params);
	var fun_des = $("#fun_des").val().trim();
	if (fun_des == "" || fun_des == null) {
		fun_des = " ";
	}
	if (fun_params == "" || fun_params == null) {
		fun_params = " ";
	}
	if (fun_params_id == "" || fun_params_id == null) {
		fun_params_id = " ";
	}
	$("#fun_name").val("");
	$("#fun_des").val("");
	var signature = $("#fun_signature").val().trim();
	if (signature == "" || signature == null) {
		signature = $("#return_type option:selected").text().trim() + " "
		+ fun_name + "(" + params.join(",") + ");";
	}
	$("#fun_signature").val("");
	
	$("#list")
			.append(
					'<div class="controls">'
							+ '<input type="hidden" name="re_type" value="'
							+ $("#return_type option:selected").val().trim()
							+ '">'
							+ '<input type="hidden" name="f_name" value="'
							+ fun_name
							+ '">'
							+ '<input type="hidden" name="f_params_id" value="'
							+ fun_params_id
							+ '">'
							+ '<input type="hidden" name="f_params" value="'
							+ fun_params
							+ '">'
							+ '<input type="hidden" name="f_sig" value="'+signature + '">'
							+ '<input type="hidden" name="f_description" value="'
							+ fun_des
							+ '">'
							+ '<code>'
							+ signature
							+ '</code><button style="margin-left:50px;margin-bottom:5px" type="button" class="btn" onclick="deltr(this)">删除</button></div>');
	$("#addInterface").dialog('close');
	refreshAddDiv();
}

function valid() {
	$("#help_name").val("英文");
	$("#help_interface").val("");
	$("#div_interface").removeClass("control-group error");
	$("#div_interface").addClass("control-group");
	$("#div_name").removeClass("control-group error");
	$("#div_name").addClass("control-group");
	var name = $("#name").val().trim();

	var interfaces = $("#list").children().length;

	if (name == "") {
		$("#div_name").removeClass("control-group");
		$("#div_name").addClass("control-group error");
		document.getElementById("help_name").innerHTML = "英文,接口名称不能为空";
		return false;
	}
	if (interfaces == 0) {
		$("#div_interface").removeClass("control-group");
		$("#div_interface").addClass("control-group error");
		document.getElementById("help_interface").innerHTML = "请为该接口添加函数";
		return false;
	}
	return true;
}

function goback() {
	history.go(-1);
}